package com.macro.mall.portal.service.impl;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.mapper.PmsProductMapper;
import com.macro.mall.mapper.SmsHomeAdvertiseMapper;
import com.macro.mall.mapper.SmsPopupAdvertiseMapper;
import com.macro.mall.model.*;
import com.macro.mall.portal.dto.SmsPopupAdvertiseDto;
import com.macro.mall.portal.service.SmsAdvertiseService;
import com.macro.mall.portal.service.UmsMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 弹窗广告管理Service
 * Created by 高强 on 2020/8/28.
 */
@Service
public class SmsAdvertiseServiceImpl implements SmsAdvertiseService {

    @Autowired
    private SmsPopupAdvertiseMapper advertiseMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private SmsHomeAdvertiseMapper homeAdvertiseMapper;
    @Autowired
    private UmsMemberService memberService;

    @Override
    public CommonResult<SmsPopupAdvertiseDto> getAdvertise() {
        UmsMember member = memberService.getCurrentMember();
        if(member==null){
            return CommonResult.success(new SmsPopupAdvertiseDto());
        }
        SmsPopupAdvertiseExample example = new SmsPopupAdvertiseExample();
        if(member.getStoreId()!=null){
            example.createCriteria().andStoreIdEqualTo(member.getStoreId());
        }
        List<SmsPopupAdvertise> smsPopupAdvertises = advertiseMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(smsPopupAdvertises)){
            SmsPopupAdvertise advertise = smsPopupAdvertises.get(0);
            SmsPopupAdvertiseDto result = new SmsPopupAdvertiseDto();
            BeanUtils.copyProperties(advertise,result);
            PmsProduct product = productMapper.selectByPrimaryKey(advertise.getProductId());
            if ((product.getPublishStatus() != null && product.getPublishStatus().equals(1)) && (product.getDeleteStatus() != null && product.getDeleteStatus().equals(0))) {
                result.setProductStatus(0);
            }else {
                result.setProductStatus(1);
            }
            if(advertise.getStatus() == 0){
                return CommonResult.success(new SmsPopupAdvertiseDto());
            }
            return CommonResult.success(result);
        }else{
            return CommonResult.success(new SmsPopupAdvertiseDto());
        }

    }


    @Override
    public PmsProduct getProduct4Advertise(Long storeId, Long productId) {
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andStoreIdEqualTo(storeId);
        example.createCriteria().andIdEqualTo(productId);
        List<PmsProduct> pmsProductsList = productMapper.selectByExample(example);
        return pmsProductsList.get(0);
    }

    @Override
    public List<SmsHomeAdvertise> getCarouselAdvertise(Long storeId) {
        //轮播图显示几条广告的计数
        int count = 0;
        SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample();
        example.createCriteria().andStoreIdEqualTo(storeId);
        //按照开始时间进行排序
        example.setOrderByClause("start_time");
        List<SmsHomeAdvertise> smsHomeAdvertiseList = homeAdvertiseMapper.selectByExample(example);
        List<SmsHomeAdvertise> result = new ArrayList<>();
        if(smsHomeAdvertiseList != null && smsHomeAdvertiseList.size() > 0){
            for (int i = smsHomeAdvertiseList.size(); i == 0; i--) {
                result.add(smsHomeAdvertiseList.get(i));
                count++;
                if(count == 3){
                    break;
                }
            }
        }
        return result;
    }
}
